package com.captain.lc.数组;

import java.util.HashMap;
import java.util.Map;

/**
 * Des:
 *
 * @author XL
 * @Date 2021/12/8 8:32
 */
public class 两数之和 {

    public static void main(String[] args) {
        for (int i : new 两数之和().twoSum(new int[]{3,2,4}, 6)) {
            System.out.println(i);
        }
    }

    public int[] twoSum(int[] nums, int target) {
        int n = nums.length - 1;
        Map<Integer, Integer> checkMap = new HashMap<>(nums.length);
        for (int i = 0; i <= n; i++) {
            int key = nums[i];
            int targetKey = target - key;
            if (checkMap.containsKey(targetKey)) {
                return new int[]{checkMap.get(targetKey),i};
            }
            checkMap.put(key, i);
        }
        return null;
    }
}
